/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class ImageViewer extends EscapeDialog {

	private static final long serialVersionUID = 8198907730668899160L;
	private ImagePanel p;
	//private boolean scaleImage = false;
	//private JButton scaleBtn;
	private Image image;
	
	public ImageViewer(Component comp) {
		this(comp, null);
	}
	
	public ImageViewer(Component comp, File file) {
		super(SwingUtilities.windowForComponent(comp));
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		setMinimumSize(new Dimension(200, 200));
		setPreferredSize(new Dimension(600, 480));
		setSize(getPreferredSize());
		
		setLayout(new BorderLayout());
		p = new ImagePanel(image);
		p.setScale(false);
		p.setBorder(new EmptyBorder(0,0,0,0));
		JScrollPane scroll = new JScrollPane();
		scroll.setViewportView(p);
		add(scroll, BorderLayout.CENTER);
		
		setImageFile(file);
		
   	/*scaleBtn = new JButton(Messages.getString(
				"PearReview.Scribe.findings.original_size"));
		scaleBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (scaleImage) {
					p.setScale(false);
					scaleBtn.setText(Messages.getString(
							"PearReview.Scribe.findings.scale"));
					scaleImage = false;
					validate();
				} else {
					p.setScale(true);
					scaleBtn.setText(Messages.getString(
							"PearReview.Scribe.findings.original_size"));
					scaleImage = true;
					validate();
				}
			}
			
		});
		
		add(scaleBtn, BorderLayout.SOUTH);
		*/
		
		setLocationRelativeTo(getOwner());
	}
	
	public void setImageFile(File file) {
		try {
			if(file == null)
				return;
			setTitle(file.getAbsolutePath());
			image = ImageIO.read(file);
			p.setImage(image);
			p.repaint();
		} catch (IOException e) {
			// TODO error handling?!
			e.printStackTrace();
		}
	}
}
